﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SevenInvaders.common;

namespace SevenInvaders.LEVEL_JACK
{
    class Jack_Ship : Player
    {
        public animation Ship_Anim;
        public animation Ship_Danger;
        public Boolean naoAnima = false;

        public Vector2 posicao_Inicial;

        public int HP = 3;
        public int Vidas = 3;

        Temporizador tempo;

        public Jack_Ship(Texture2D texture, Vector2 position, int player, GameWindow gameWindow, ContentManager content)
            : base(texture, position, player, gameWindow, content)
        {
            Ship_Anim = new animation();
            Ship_Anim.frame.X = 0;
            Ship_Anim.frame.Y = 0;
            Ship_Anim.frame.Width = 108;
            Ship_Anim.frame.Height = 44;
            Ship_Anim.frames = 2;
            Ship_Anim.fps = 8;

            Ship_Danger = new animation();
            Ship_Danger.frame.X = 0;
            Ship_Danger.frame.Y = 44;
            Ship_Danger.frame.Width = 108;
            Ship_Danger.frame.Height = 44;
            Ship_Danger.frames = 2;
            Ship_Danger.fps = 8;

            current_animation = Ship_Anim;

            posicao_Inicial = position;

            tempo = new Temporizador(1000.0f);
        }

        public void Update(GameTime gameTime, KeyboardState teclado, KeyboardState teclado_anterior)
        {
            #region Pausa ou Ativa a animação
            //===============================

            if (naoAnima == true)
            {
                Ship_Anim.fps = 0;
                Ship_Danger.fps = 0;
            }
            else
            {
                Ship_Anim.fps = 8;
                Ship_Danger.fps = 8;
            }
            #endregion

            #region Troca a animação de acordo com o estado da nave
            if (HP <= 1)
            {
                current_animation = Ship_Danger;
            }
            else
            {
                current_animation = Ship_Anim;
            } 
            #endregion

            #region Controle da nave
            // Controlar a Nave

            // Movimento
            if (teclado.IsKeyDown(Keys.Left))
            {
                this.PositionX -= 3;
            }
            if (teclado.IsKeyDown(Keys.Right))
            {
                this.PositionX += 5;
            }
            if (teclado.IsKeyDown(Keys.Up))
            {
                this.PositionY -= 4;
            }
            if (teclado.IsKeyDown(Keys.Down))
            {
                this.PositionY += 4;
            }
            
            // Limite de tela
            if (this.PositionX + this.Ship_Anim.frame.Width > Window.ClientBounds.Width)
            {
                this.PositionX = Window.ClientBounds.Width - this.Ship_Anim.frame.Width;
            }
            if (this.PositionX < 0)
            {
                this.PositionX = 0;
            }
            if (this.PositionY + this.Ship_Anim.frame.Height> Window.ClientBounds.Height)
            {
                this.PositionY = Window.ClientBounds.Height - this.Ship_Anim.frame.Height;
            }
            if (this.PositionY < 0)
            {
                this.PositionY = 0;
            }
            #endregion

            #region Controle de tiro
            if (teclado.IsKeyDown(Keys.Space) && !teclado_anterior.IsKeyDown(Keys.Space))
            {
                Shoot.list.Add(new Shoot(Level_Jack.jack_tiro, new Vector2(PositionX + (this.Ship_Anim.frame.Width - 20), PositionY + (this.Ship_Anim.frame.Height - 20)),
                Shoot.Direction.RIGHT, new Vector2(10,0)));
            }
            #endregion
        }
    }
}
